#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author:    thepoy
# @Email:     thepoy@163.com
# @File Name: postgresql_psycopg_async.py
# @Created:   2021-12-27 17:09:31
# @Modified:  2021-12-28 15:15:01

import sys
import asyncio
import psycopg

is_windows = sys.platform == "win32"
if is_windows:
    asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())  # type: ignore


async def test():
    async with await psycopg.AsyncConnection.connect(
        # 如果在 windows 中连接太慢，可以尝试添加 gssencmode=disable 属性
        "host=xxxx user=xxxx password=xxxx dbname=xxxx sslmode=disable"
    ) as conn:
        async with conn.cursor() as cur:
            await cur.execute("SELECT id FROM items LIMIT 1;")
            row = await cur.fetchone()
            if not row:
                raise Exception("没有结果")
            print(f"result: id={row[0]}")


if __name__ == "__main__":
    asyncio.run(test())
